﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;

namespace RN.ERP.Web.Enterprise
{
    public partial class EnterpriseMarketsInfo : BasePage
    {
        private EnterpriseService.EnterpriseServiceClient enterpriseServiceClient = new EnterpriseService.EnterpriseServiceClient();
        private BasicService.BasicServiceClient basicServiceClient = new BasicService.BasicServiceClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindMarketInfo();
        }

        public string GetAreaTitle(object areaCode)
        {
            string title = "";
            if (areaCode != null)
            {
                string code = areaCode.ToString();
                if (code.Length >= 2)
                {
                    var areaInfos = JsonConvert.DeserializeObject<List<a_AreaInfo>>(Application[ApplicationConstant.APPLICATION_AREA].ToString());
                    var currentArea = areaInfos.Where(a => code.StartsWith(a.Code)).FirstOrDefault();
                    title = currentArea == null ? "" : currentArea.Title;
                }
            }
            return title;
        }

        public string GetSubAreaTitle(object areaCode)
        {

            string title = "";
            if (areaCode != null)
            {
                string code = areaCode.ToString();
                if (code.Length > 2)
                {
                    var subAreaInfos = JsonConvert.DeserializeObject<List<ModelService.a_AreaSubInfo>>(basicServiceClient.GetQu(code.Substring(0, 2)));
                    var currentSubArea = subAreaInfos.Where(a => a.SortCode == areaCode).FirstOrDefault();
                    title = currentSubArea == null ? "" : currentSubArea.Title;
                }
            }
            return title;
        }

        public string GetEmbranchmentFlow(object areaCode)
        {
            string flowTitle = "";
            if (areaCode == null)
            {
                return flowTitle;
            }
            string strAreaCode = areaCode.ToString();
            string code = strAreaCode.Length > 2 ? strAreaCode.Substring(0, 2) : strAreaCode;
            SessionUserInfo user = (SessionUserInfo)Session[SessionConstant.SESSION_USERINFO];
            Tb_Corp_CompanyInfo corpInfo = new Tb_Corp_CompanyInfo();
            corpInfo.CorpUserGuid = Guid.Parse(user.CorpUserGuid);
            var flow = JsonConvert.DeserializeObject<Tb_FlowCode>(enterpriseServiceClient.GetCorpFlowCode(JsonConvert.SerializeObject(corpInfo), code));
            if (flow != null)
            {
                flowTitle = string.IsNullOrEmpty(flow.FlowCode) ? "" : flow.FlowCode;
            }
            return flowTitle;
        }

        public string GetFlowTitle(object areaCode)
        {
            if (areaCode == null)
            {
                return "";
            }
            string strAreaCode = areaCode.ToString();
            string code = strAreaCode.Length > 2 ? strAreaCode.Substring(0, 2) : strAreaCode;
            QueryCompanyInfoCondition queryCompanyInfoCondition =
                new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(GetCorpUserGuid()), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            var jcorp = enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), UserInfo.CurrentArea);
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(jcorp);
            string classid = corpInfo.classID.ToString();
            return basicServiceClient.GetFlowCodeTitle(code, classid, GetEmbranchmentFlow(code));
        }

        private void BindMarketInfo()
        {
            string corpUserGuid = GetCorpUserGuid();
            Tb_Corp_CompanyInfo corp = new Tb_Corp_CompanyInfo();
            Guid guid=Guid.Empty;
            if (Guid.TryParse(corpUserGuid, out guid))
            {
                corp.CorpUserGuid = guid;
                var Embranchments = enterpriseServiceClient.GetCorpEmbranchments(JsonConvert.SerializeObject(corp));
                var CorpEmbranchments = JsonConvert.DeserializeObject<IList<Tb_Corp_Embranchment>>(Embranchments);
                rptMarket.DataSource = CorpEmbranchments;
                rptMarket.DataBind();
                if (CorpEmbranchments == null || CorpEmbranchments.Count < 1)
                {
                    ////尚未在省内指定市场范围
                    tips.Visible = true;
                }
            }
            if (string.IsNullOrEmpty(UserInfo.CurrentArea))
            {
                ltrCurrentMarket.Text = "未添加";
            }
            else
            {
                ltrCurrentMarket.Text = GetAreaTitle(UserInfo.CurrentArea);

            }
            QueryCompanyInfoCondition queryCompanyInfoCondition =
                new QueryCompanyInfoCondition() { CorpUserGuid = new ParameterValue<Guid>() { V = Guid.Parse(GetCorpUserGuid()), O = OptEnum.Equal } };
            List<QueryCompanyInfoCondition> queryCompanyInfoList = new List<QueryCompanyInfoCondition>();
            queryCompanyInfoList.Add(queryCompanyInfoCondition);
            var jcorp = enterpriseServiceClient.GetCompanyInfo(JsonConvert.SerializeObject(queryCompanyInfoList), UserInfo.CurrentArea);
            var corpInfo = JsonConvert.DeserializeObject<Tb_Corp_CompanyInfo>(jcorp);
            string classid = corpInfo.classID.ToString();
            ltrCurrentMarketFlow.Text = basicServiceClient.GetFlowCodeTitle(UserInfo.CurrentArea, classid, GetEmbranchmentFlow(UserInfo.CurrentArea));
        }

        protected void btnAddMarket_Click(object sender, EventArgs e)
        {
            string areaCode = AreaCodeAdd.AreaCode;
            if (string.IsNullOrEmpty(areaCode))
            {
                RadWindowManager1.RadAlert("请选择市场范围",300,150,"提示","");
                return;
            }
            UserInfo.CurrentArea = areaCode;
            Session[SessionConstant.SESSION_USERINFO] = UserInfo;
            Response.Redirect("/Enterprise/EnterpriseCorpInfo.aspx");
        }

        private string GetCorpUserGuid()
        {
            string corpUserGuid = Request.QueryString["corpUserGuid"];
            if (string.IsNullOrEmpty(corpUserGuid))
            {
                corpUserGuid = UserInfo.CorpUserGuid;
            }
            return corpUserGuid;
        }
    }
}